package com.hs.user.controller;

import com.hs.user.entity.Student;
import com.hs.user.service.StudentSercvice;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by sheng on 2017/7/10.
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private StudentSercvice studentSercvice;

//    @RequestMapping("/test")
//    public ModelAndView testUser(){
//        ModelAndView model = new ModelAndView();
//        model.setViewName("user");
//        User user = new User();
//        user.setId(11);
//        user.setName("laowang");
//        model.addObject("user",user);
//        return model;
//    }

//    @RequestMapping("/test")
//    public String testUser(ModelMap modelMap){
//        User user = new User();
//        user.setId(13);
//        user.setName("laowang2");
//        modelMap.addAttribute("user",user);
//        return "user";
//    }

//    @RequestMapping("/test/{id}/{name}")
//    public String testUser(ModelMap modelMap, @PathVariable("id") Integer id, @PathVariable("name") String name){
//
//        System.out.println(id);
//        System.out.println(name);
//
//        User user = new User();
//        user.setId(13);
//        user.setName("laowang2");
//        modelMap.addAttribute("user",user);
//        return "user";
//    }

//    @RequestMapping("/test")
//    public String testUser(ModelMap modelMap, HttpServletRequest request){
//
//        String id = request.getParameter("id");
//        String name = request.getParameter("name");
//        System.out.println(id);
//        System.out.println(name);
//
//        User user = new User();
//        user.setId(13);
//        user.setName("laowang2");
//        modelMap.addAttribute("user",user);
//        return "user";
//    }

//    @RequestMapping("/test")
//    public String testUser(ModelMap modelMap,User user){
//        System.out.println(user.toString());
////        User user = new User();
////        user.setId(13);
////        user.setName("laowang2");
//        modelMap.addAttribute("user",user);
//        return "user";
//    }

//    @RequestMapping("/test")
//    @ResponseBody
//    public User testUser(){
//        User user = new User();
//        user.setId(13);
//        user.setName("laowang2");
//        return user;
//    }

    @RequestMapping("/test")
    public String testUser(@RequestBody Student student){







        studentSercvice.createStudent(student);
        System.out.println(student);
        return "user";
    }

    @RequestMapping("/demo")
    public String demoUser(){
        return "demo";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();
        File uploadF = new File("/"+name);
        file.transferTo(uploadF);
        return "demo";
    }

    @RequestMapping("/downLoad")
    public ResponseEntity<byte[]> downLoad() throws IOException {
        String path = "D:/SpringMVC.png";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",path);  //attachment(附件)，告诉浏览器文件在服务器的位置
        headers.setContentType(MediaType.IMAGE_PNG);   //说明文件的类型
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
